package com.fx.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * des:
 *
 * @author fxiao
 * @date 2020/12/10 16:46
 */
@Entity(name = "t_role")
public class Role {
    /**
     *
     * @param code
     * @param name
     * @param resources
     */
    public Role(String code, String name, Resource ... resources) {
        this.code = code;
        this.name = name;
        this.resources=new ArrayList<>(Arrays.asList(resources));
    }

    /**
     *
     * @param code
     * @param name
     */
    public Role(String code, String name) {
        this.code = code;
        this.name = name;
    }
    public Role(){

    }
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(unique = true)
    private String code;
    private String name;
    @ManyToMany(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)
    @JoinTable(
            name="userRole"
            ,joinColumns = {@JoinColumn(name="roleId")}
            ,inverseJoinColumns = {@JoinColumn(name = "userId")}
            )
    @JsonIgnore
    private List<User> users;
    @ManyToMany(fetch = FetchType.EAGER,cascade=CascadeType.REFRESH)
    @JoinTable(
            name = "roleResource"
            ,joinColumns = {@JoinColumn(name = "roleId")}
            ,inverseJoinColumns = {@JoinColumn(name = "resourceId")}
    )
    private List<Resource>resources;
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public void setUsers(User ... arrUsers) {
        this.users = new ArrayList<>(Arrays.asList(arrUsers));
    }

    public List<Resource> getResources() {
        return resources;
    }

    public void setResources(List<Resource> resources) {
        this.resources = resources;
    }
    // 原来getter方法不能重载，会在反序列化的时候出错，所以这里不能叫setResources
    // 坑死我了，找了一下午错，原来在这里
    public void setResourcesByArrResource(Resource ... arrResources){
        this.resources=new ArrayList<>(Arrays.asList(arrResources));
    }
}